package com.atwork.wuhua.mvp.presenter;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.widget.TextView;

import com.atwork.wuhua.base.BasePresenter;
import com.atwork.wuhua.base.BaseReq;
import com.atwork.wuhua.base.MyApplication;
import com.atwork.wuhua.bean.UserBean;
import com.atwork.wuhua.bean.VerifyBean;
import com.atwork.wuhua.mvp.Callback;
import com.atwork.wuhua.mvp.DataModel;
import com.atwork.wuhua.mvp.Token;
import com.atwork.wuhua.mvp.view.ILoginActivity;
import com.atwork.wuhua.utils.LogUtils;
import com.atwork.wuhua.utils.TimeUtil;
import com.atwork.wuhua.utils.UserUtil;

import cn.jpush.android.api.JPushInterface;

public class LoginPresenter extends BasePresenter<ILoginActivity> {
    private String verifyCode;

    public void getVerify(Activity activity, TextView tvSendCode, final String mobile) {
        if (mobile.length() == 0) {
            showToast("手机号不能为空");
            return;
        }
        if (mobile.trim().length() != 11 || !mobile.substring(0, 1).equals("1")) {

            showToast("手机号格式不正确");
            return;
        }
        TimeUtil.CountDownTime(activity, tvSendCode);
        BaseReq req = new BaseReq();
        req.setKey("mobile", mobile);
        req.setKey("type", "login");

        if (isViewAttached() && req != null) {
            DataModel.request(Token.API_VERIFY)
                    .params(req, false)
                    .execute(new Callback<VerifyBean>() {


                        @Override
                        public void onSuccess(String msg, VerifyBean data) {
                            showToast("已发送验证码");
                            if (data.getData().size() > 0) {
                                verifyCode = data.getData().get(0).toString();
                                if (mobile.equals("18144113262")) {
                                    getView().setVerify(verifyCode);
                                    LogUtils.e("verifyCode==>" + verifyCode);
                                }
                            }
                        }

                        @Override
                        public void onFailure(String msg) {
                            showToast(msg);
                        }

                        @Override
                        public void onError() {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }


    //登录
    public void login(String mobile, String verify) {
        LogUtils.e("mobile==>" + mobile);
        LogUtils.e("verify==>" + verify);
        if (mobile.length() == 0) {
            showToast("请输入正确的手机号");
            return;
        }
        if (mobile.trim().length() != 11 || !mobile.substring(0, 1).equals("1")) {
            showToast("请输入正确的手机号");
            return;
        }
        if (!verify.equals(verifyCode)) {
            showToast("请输入6位正确验证码");
            return;
        }
        showLoading();
        BaseReq req = new BaseReq();
        req.setKey("mobile", mobile);
        req.setKey("verify", verify);
        req.setKey("registration_id", JPushInterface.getRegistrationID(getView().getContext()));
        req.setKey("device_type", "2");
        if (isViewAttached()) {
            DataModel.request(Token.API_LOGIN)
                    .params(req)
                    .execute(new Callback<UserBean>() {
                        @Override
                        public void onSuccess(String msg, UserBean data) {
                            UserUtil.loginSuccess(getView().getContext(), data);
                            showToast(msg);
                            getView().setLogin();
                            hideLoading();
                        }

                        @Override
                        public void onFailure(String msg) {
                            showToast(msg);
                            hideLoading();

                        }

                        @Override
                        public void onError() {

                        }

                        @Override
                        public void onComplete() {

                        }
                    });
        }
    }


}
